package com.fengchao.api.controller.v1;

import com.alibaba.fastjson.JSON;
import com.fengchao.api.service.SearchService;
import com.fengchao.api.service.UsrService;
import com.fengchao.model.entity.comm.CustomBizException;
import com.fengchao.model.entity.comm.Message;
import com.fengchao.model.entity.comm.ResponseParameter;
import com.fengchao.util.common.CommonValidator;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.fengchao.api.config.Constant.GET_SUCCESS;
import static com.fengchao.api.config.Constant.PARAMETER_IS_NOT_COMPLETE;

/**
 * Created by wangnian on 2016/6/30.
 */
@RequestMapping("/v1/search")
@RestController
public class SearchController  {

    @Autowired
    private SearchService searchService;
    @Autowired
    private UsrService usrService;


    private static Logger logger = LoggerFactory.getLogger(SearchController.class);

    /**
     * 获取艺人的显示名字模糊搜索
     *
     * @return
     */
    @ApiOperation(value = "(APP/WEB)模糊搜索艺人的名字", notes = "返回参数:</br>成功:</br>{status: SUCCESS, </br>message: 获取成功,</br>data:{</br>&nbsp;&nbsp;info: [</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;nationality: 菲律宾(国籍),</br>&nbsp;&nbsp;&nbsp;&nbsp;nameDisplay: 音乐人王念(艺人展示名称)," +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;appImage: http://swarm-dev.b0.upaiyun.com/bci/artist_head_icon_pc/cbebe907a05af0be58baad45a15ca3d0.png (app的详情页头部图片地址),</br>&nbsp;&nbsp;&nbsp;&nbsp;id: 32(艺人ID)</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>}" +
            "</br>失败:</br>{status: FAILURE, message: 参数不全}")
    @RequestMapping(value = "/getArtistLikeKeyWords/{keyWords}/{page}/{count}", method = RequestMethod.GET)
    public ResponseParameter getArtistLikeKeyWords(@ApiParam(value = "(APP/WEB必填)搜索的关键字", required = true) @PathVariable("keyWords") String keyWords,
                                               @ApiParam(value = "(APP/WEB必填)第几页", required = true) @PathVariable("page") Integer page,
                                               @ApiParam(value = "(APP/WEB必填)显示多少行", required = true) @PathVariable("count") Integer count) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("模糊搜索艺人的名字请求参数= keyWords:{} page:{},count:{}", keyWords, page, count);
        if (!CommonValidator.isStringsNotNull(keyWords)) {
            throw new CustomBizException(PARAMETER_IS_NOT_COMPLETE);
        }
        searchService.setKeyWord(keyWords);
        List<Map<String, Object>> resultList = searchService.findArtistMapByLikeName(keyWords, page, count);
        Map<Object, Object> map = new HashMap<>();
        map.put("info", resultList);
        responseParameter.setData(map);
        logger.info("模糊搜索艺人的名字响应参数={}", JSON.toJSON(responseParameter));
        return responseParameter;
    }

    /**
     * 获取众筹的显示名字模糊搜索
     *
     * @return
     */
    @ApiOperation(value = "(APP/WEB)模糊搜索众筹的名字", notes = "返回参数:</br>成功:</br>{status: SUCCESS, </br>message: 获取成功,</br>data:{</br>&nbsp;&nbsp;info: [</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;image: http://aaa.png (app众筹列表的背景图片地址),</br>&nbsp;&nbsp;&nbsp;&nbsp;title: aaa(众筹标题),</br>&nbsp;&nbsp;&nbsp;&nbsp;time: 1472024300000(众筹开始时间)," +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;city: 上海(众筹的城市),</br>&nbsp;&nbsp;&nbsp;&nbsp;id: 1(众筹ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;artistName: aaa(艺人名称)</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>}" +
            "</br>失败:</br>{status: FAILURE, message: 参数不全}")
    @RequestMapping(value = "/getCrowdfundingLikeKeyWords/{keyWords}/{page}/{count}", method = RequestMethod.GET)
    public ResponseParameter getCrowdfundingLikeKeyWords(@ApiParam(value = "(APP/WEB必填)搜索的关键字", required = true) @PathVariable("keyWords") String keyWords,
                                                     @ApiParam(value = "(APP/WEB必填)第几页", required = true) @PathVariable("page") Integer page,
                                                     @ApiParam(value = "(APP/WEB必填)显示多少行", required = true) @PathVariable("count") Integer count) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("模糊搜索众筹的名字请求参数= keyWords:{} page:{},count:{}", keyWords, page, count);
        if (!CommonValidator.isStringsNotNull(keyWords)) {
            throw new CustomBizException(PARAMETER_IS_NOT_COMPLETE);
        }
        searchService.setKeyWord(keyWords);
        List<Map<String, Object>> resultList = searchService.findCrowdfundingMapByLikeName(keyWords, page, count);
        Map<Object, Object> map = new HashMap<>();
        map.put("info", resultList);
        responseParameter.setData(map);
        logger.info("模糊搜索众筹的名字响应参数={}", JSON.toJSON(responseParameter));
        return responseParameter;
    }

    /**
     * 获取众筹的显示名字模糊搜索
     *
     * @return
     */
    @ApiOperation(value = "(APP/WEB)模糊搜索歌曲的名字", notes = "返回参数:</br>成功:</br>{status: SUCCESS, </br>message: 获取成功,</br>data:{</br>&nbsp;&nbsp;info: [</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;albumName: Make It Big(专辑名称),</br>&nbsp;&nbsp;&nbsp;&nbsp;albumId: 1(专辑ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;artistId: 1(艺人ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;songsName: 王念唱的歌1(歌曲名称),</br>&nbsp;&nbsp;&nbsp;&nbsp;songsId: 1(歌曲ID)</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>}" +
            "</br>失败:</br>{status: FAILURE, message: 参数不全}")
    @RequestMapping(value = "/getSongsLikeKeyWords/{keyWords}/{page}/{count}", method = RequestMethod.GET)
    public ResponseParameter getSongsLikeKeyWords(@ApiParam(value = "(APP/WEB必填)搜索的关键字", required = true) @PathVariable("keyWords") String keyWords,
                                              @ApiParam(value = "(APP/WEB必填)第几页", required = true) @PathVariable("page") Integer page,
                                              @ApiParam(value = "(APP/WEB必填)显示多少行", required = true) @PathVariable("count") Integer count) {
        logger.info("模糊搜索歌曲的名字请求参数= keyWords:{} page:{},count:{}", keyWords, page, count);
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        if (!CommonValidator.isStringsNotNull(keyWords)) {
            throw new CustomBizException(PARAMETER_IS_NOT_COMPLETE);
        }
        searchService.setKeyWord(keyWords);
        List<Map<String, Object>> resultList = searchService.findMriSongsByLinkName(keyWords, page, count);
        Map<Object, Object> map = new HashMap<>();
        map.put("info", resultList);
        responseParameter.setData(map);
        logger.info("模糊搜索歌曲的名字响应参数={}", JSON.toJSON(responseParameter));
        return responseParameter;
    }

    /**
     * 获取专辑的名字模糊搜索
     *
     * @return
     */
    @ApiOperation(value = "(APP/WEB)获取专辑的名字模糊搜索", notes = "返回参数:</br>成功:</br>{status: SUCCESS, </br>message: 获取成功,</br>data:{</br>&nbsp;&nbsp;info:[</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;albumName: 童谣(专辑名称),</br>&nbsp;&nbsp;&nbsp;&nbsp;albumId: 1(专辑ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;artistId: 1(艺人ID)</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>}" +
            "</br>失败:</br>{status: FAILURE, message: 参数不全}")
    @RequestMapping(value = "/getAlbumLikeKeyWords/{keyWords}/{page}/{count}", method = RequestMethod.GET)
    public ResponseParameter getAlbumLikeKeyWords(@ApiParam(value = "(APP/WEB必填)搜索的关键字", required = true) @PathVariable("keyWords") String keyWords,
                                                  @ApiParam(value = "(APP/WEB必填)第几页", required = true) @PathVariable("page") Integer page,
                                                  @ApiParam(value = "(APP/WEB必填)显示多少行", required = true) @PathVariable("count") Integer count) {
        logger.info("获取专辑的名字模糊搜索请求参数= keyWords:{} page:{},count:{}", keyWords, page, count);
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        if (!CommonValidator.isStringsNotNull(keyWords)) {
            throw new CustomBizException(PARAMETER_IS_NOT_COMPLETE);
        }
        searchService.setKeyWord(keyWords);
        List<Map<String, Object>> resultList = searchService.findAlbumByLinkName(keyWords, page, count);
        Map<Object, Object> map = new HashMap<>();
        map.put("info", resultList);
        responseParameter.setData(map);
        logger.info("获取专辑的名字模糊搜索响应参数={}", JSON.toJSON(responseParameter));
        return responseParameter;
    }

    /**
     * 模糊搜索
     *
     * @return
     */
    @ApiOperation(value = "(APP/WEB)模糊搜索", notes = "返回参数:</br>成功:</br>{status: SUCCESS, </br>message: 获取成功,</br>data:{</br>&nbsp;&nbsp;albums: [</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;albumName: 童谣(专辑名称),</br>&nbsp;&nbsp;&nbsp;&nbsp;albumId: 1(专辑ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;artistId: 1(艺人ID)</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]," +
            "</br>&nbsp;&nbsp;artists: [</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;nationality: 菲律宾(国籍),</br>&nbsp;&nbsp;&nbsp;&nbsp;nameDisplay: 音乐人王念(艺人展示名称),</br>&nbsp;&nbsp;&nbsp;&nbsp;appImage: http://swarm-dev.b0.upaiyun.com/bci/artist_head_icon_pc/cbebe907a05af0be58baad45a15ca3d0.png (app的详情页头部图片地址),</br>&nbsp;&nbsp;&nbsp;&nbsp;id: 32(艺人ID)</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]," +
            "</br>&nbsp;&nbsp;songs: [</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;albumName: Make It Big(专辑名称),</br>&nbsp;&nbsp;&nbsp;&nbsp;albumId: 1(专辑ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;artistId: 1(艺人ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;songsName: 王念唱的歌1(歌曲名称),</br>&nbsp;&nbsp;&nbsp;&nbsp;songsId: 1(歌曲ID)</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]," +
            "</br>&nbsp;&nbsp;crowdfundings: [</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;image: http://aaa.png (app众筹列表的背景图片地址),</br>&nbsp;&nbsp;&nbsp;&nbsp;title: aaa(众筹标题),</br>&nbsp;&nbsp;&nbsp;&nbsp;time: 1472024300000(众筹开始时间),</br>&nbsp;&nbsp;&nbsp;&nbsp;city: 上海(众筹的城市),</br>&nbsp;&nbsp;&nbsp;&nbsp;id: 1(众筹ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;artistName: aaa(艺人名称)</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>}" +
        "</br>失败:</br>{status: FAILURE, message: 参数不全}")
    @RequestMapping(value = "/getAllLikeKeyWords/{keyWords}/{page}/{count}", method = RequestMethod.GET)
    public ResponseParameter getAllLikeKeyWords(@ApiParam(value = "(APP/WEB必填)搜索的关键字", required = true) @PathVariable("keyWords") String keyWords,
                                                @ApiParam(value = "(APP/WEB必填)第几页", required = true) @PathVariable("page") Integer page,
                                                @ApiParam(value = "(APP/WEB必填)显示多少行", required = true) @PathVariable("count") Integer count) {
        logger.info("模糊搜索请求参数= keyWords:{} page:{},count:{}", keyWords, page, count);
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        if (!CommonValidator.isStringsNotNull(keyWords)) {
            throw new CustomBizException(PARAMETER_IS_NOT_COMPLETE);
        }
        searchService.setKeyWord(keyWords);
        List<Map<String, Object>> songs = searchService.findMriSongsByLinkName(keyWords, page, count);
        List<Map<String, Object>> artists = searchService.findArtistMapByLikeName(keyWords, page, count);
        List<Map<String, Object>> crowdfundings = searchService.findCrowdfundingMapByLikeName(keyWords, page, count);
        List<Map<String, Object>> albums = searchService.findAlbumByLinkName(keyWords, page, count);
        Map<Object, Object> map = new HashMap<>();
        map.put("songs", songs);
        map.put("artists", artists);
        map.put("crowdfundings", crowdfundings);
        map.put("albums", albums);
        responseParameter.setData(map);
        logger.info("模糊搜索响应参数={}", JSON.toJSON(responseParameter));
        return responseParameter;
    }

    /**
     * 热门搜索
     *
     * @return
     */
    @ApiOperation(value = "(APP/WEB)获取热门搜索", notes = "返回参数:</br>成功:</br>{status: SUCCESS, </br>message: 获取成功,</br>data:{</br>&nbsp;&nbsp;info: [</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;keyWord: huihui (热门搜索的关键词)</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>}" +
            "</br>失败:</br>{status: FAILURE, message: 参数不全}")
    @RequestMapping(value = "/getKeyWords/{page}/{count}", method = RequestMethod.GET)
    public ResponseParameter getKeyWords(@ApiParam(value = "(APP/WEB必填)第几页", required = true) @PathVariable("page") Integer page,
                                         @ApiParam(value = "(APP/WEB必填)显示多少行", required = true) @PathVariable("count") Integer count) {
        logger.info("获取热门搜索请求参数= page:{},count:{}", page, count);
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        List<Map<String, Object>> trcSearchKeyWordList = searchService.findTrcSearchKeyWordList(page, count);
        Map<Object, Object> map = new HashMap<>();
        map.put("info", trcSearchKeyWordList);
        responseParameter.setData(map);
        logger.info("获取热门搜索响应参数={}", JSON.toJSON(responseParameter));
        return responseParameter;
    }

    @RequestMapping(value = "/getArtistlinkList/{keyWords}/{page}/{count}", method = RequestMethod.GET)
    @ApiOperation(value = "(APP/WEB)艺人列表搜索", notes = "返回参数:</br>成功:</br>{status: SUCCESS, </br>message: 获取成功,</br>data:{</br>&nbsp;&nbsp;info: [</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;headIconApp: http://swarm-dev.b0.upaiyun.com/bci/artist_head_icon_app/8c35f53862c3d866085e5950efd5a0ca.png (APP端艺人头像),</br>&nbsp;&nbsp;&nbsp;&nbsp;headIconPc: http://swarm-dev.b0.upaiyun.com/bci/artist_head_icon_pc/8c35f53862c3d866085e5950efd5a0ca.png (PC端艺人头像),</br>&nbsp;&nbsp;&nbsp;&nbsp;nameZh: 王念(艺人中文名称)," +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;nameDisplay: 音乐人王念(艺人展示姓名),</br>&nbsp;&nbsp;&nbsp;&nbsp;sex: 男(艺人性别),</br>&nbsp;&nbsp;&nbsp;&nbsp;locationType: 中国大陆(所属地区,港澳台/日本/韩国/欧美/中国大陆),</br>&nbsp;&nbsp;&nbsp;&nbsp;focusCount: 2(艺人粉丝数量),</br>&nbsp;&nbsp;&nbsp;&nbsp;nameEn: aaa(艺人英文名),</br>&nbsp;&nbsp;&nbsp;&nbsp;nameEn: artistInfo(艺人简介),</br>&nbsp;&nbsp;&nbsp;&nbsp;nationality: 菲律宾(艺人国籍),</br>&nbsp;&nbsp;&nbsp;&nbsp;isFocus: 0(艺人是否被关注, 1:是, 0: 否)," +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;musicStyle: [{id: 1, typeName: 流行}] 音乐风格,</br>&nbsp;&nbsp;&nbsp;&nbsp;represent: 惺惺惜惺惺 (艺人代表作),</br>&nbsp;&nbsp;&nbsp;&nbsp;id: 32(艺人ID)</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>}" +
            "</br>失败:</br>{status: FAILURE, message: 参数不全}")
    public ResponseParameter getArtistlinkList(@ApiParam(value = "类型, APP/WEB") @RequestHeader(required = false) String type,
                                               @ApiParam(value = "用户token") @RequestHeader(required = false) String token,
                                               @ApiParam(value = "(APP/WEB必填)搜索关键字", required = true) @PathVariable("keyWords") String keyWords,
                                               @ApiParam(value = "(APP/WEB必填)第几页", required = true) @PathVariable("page") Integer page,
                                               @ApiParam(value = "(APP/WEB必填)显示多少行", required = true) @PathVariable("count") Integer count) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("艺人列表搜索请求参数=type:{} token:{} keyWords:{},page:{},count:{}", type, token, keyWords, page, count);
        if (!CommonValidator.isStringsNotNull(keyWords, page, count)) {
            throw new CustomBizException(PARAMETER_IS_NOT_COMPLETE);
        }
        Integer userId = usrService.getUserId(type, token, false);
        List<Map<String, Object>> list = searchService.findArtistByNamePyAndPage(userId, keyWords, page, count);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("info", list);
        responseParameter.setData(map);
        logger.info("艺人列表搜索响应参数= {}", JSON.toJSON(responseParameter));
        return responseParameter;
    }

    @RequestMapping(value = "/getArtistSummaryLikeNameList/{keyWords}/{page}/{count}", method = RequestMethod.GET)
    @ApiOperation(value = "(APP/WEB)模糊搜索心动艺人的列表", notes = "返回参数:</br>成功:</br>{status: SUCCESS, </br>message: 获取成功,</br>data:{</br>&nbsp;&nbsp;info: [</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;region: 菲律宾(出生地),</br>&nbsp;&nbsp;&nbsp;&nbsp;name: 音乐人王念(艺人姓名)," +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;headIcon: http://i.gtimg.cn/music/photo/mid_singer_300/p/o/0013TGkf3M29po.jpg (艺人头像),</br>&nbsp;&nbsp;&nbsp;&nbsp;id: 32(艺人ID)</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>}" +
            "</br>失败:</br>{status: FAILURE, message: 参数不全}")
    public ResponseParameter getArtistSummaryLikeNameList(@ApiParam(value = "(APP/WEB必填)类型, APP/WEB", required = true) @RequestHeader String type,
                                                          @ApiParam(value = "(APP/WEB必填)搜索关键字", required = true) @PathVariable("keyWords") String keyWords,
                                                          @ApiParam(value = "(APP/WEB必填)第几页", required = true) @PathVariable("page") Integer page,
                                                          @ApiParam(value = "(APP/WEB必填)显示多少条", required = true) @PathVariable("count") Integer count) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("模糊搜索心动艺人的列表参数= type:{},page:{},count:{}", type, page, count);
        if (!CommonValidator.isStringsNotNull(type)) {
            throw new CustomBizException(PARAMETER_IS_NOT_COMPLETE);
        }
        List<Map<String, Object>> list = searchService.getBciArtistSummaryByName(keyWords, page, count);
        Map<Object, Object> map = new HashMap<>();
        map.put("info", list);
        responseParameter.setData(map);
        logger.info("模糊搜索心动艺人的列表响应参数 = 条数:{}", list.size());
        logger.debug("模糊搜索心动艺人的列表响应参数 = {}", JSON.toJSON(responseParameter));
        return responseParameter;
    }


}
